import logging
from sessionObj import SessionObj
import uuid

class Session():
    clientList = []
    
    def createSession(self, ID, IP):
        self.deleteSession(IP)
        sess = SessionObj(ID, IP)
        self.clientList.append(sess)
        logging.info('Created session for ' + IP + ' ID: ' + ID)
        
    def deleteSession(self, IP):
        j = 0
        for i in self.clientList:
            if i.sessionIP == IP:
                self.clientList.pop(j)
            j += 1
        logging.info('Closed session(s) for ' + IP + ' .')
    
    def sessionExists(self, ID, IP):
        if ID == 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee':
            return True
        for i in self.clientList:
            if i.sessionID == ID and i.sessionIP == IP:
                return True
        return False
    
    def IDexists(self, ID):
        for i in self.clientList:
            if i.sessionID == ID:
                return True
        return False
    
    def getID(self, IP):
        for i in self.clientList:
            if i.sessionIP == IP:
                return i.sessionID
        return 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'

    def generateID(self):
        randID = str(uuid.uuid4())
        while self.IDexists(randID) == True:
            randID = str(uuid.uuid4())
        return randID